/*
 * jxUtils Project (2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.date;

import java.util.ArrayList;
import java.util.List;

import org.jxUtils.check.ParameterCheck;
import org.jxUtils.collection.CollectionUtil;
import org.jxUtils.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxUtils
 * @package org.jxUtils.date
 * @date 28/05/2013 - 19:57:50
 * @see {@link HourFormatters},<br>
 *      {@link DateFormatters},<br>
 *      {@link DateHourFormatters}
 */
public abstract class TimeFormatters {
	
	private final List<TimeFormatter> timeFormatters = new ArrayList<TimeFormatter>();
	
	/**
	 * 
	 */
	public TimeFormatters() {
	
	}
	
	/**
	 * @param timeFormatter
	 */
	public final void addFormatter(final TimeFormatter timeFormatter) {
	
		ParameterCheck.isNull(timeFormatter, "timeFormatter");
		
		//
		if( !timeFormatters.contains(timeFormatter)) {
			
			timeFormatters.add(timeFormatter);
		}
	}
	
	/**
	 * @param mask
	 * @return true if the mask is valid
	 */
	public final boolean containsMask(final String mask) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(mask, "mask");
		
		return getMasks().contains(mask);
	}
	
	/**
	 * @param pattern
	 * @return true if the pattern is valid
	 */
	public final boolean containsPattern(final String pattern) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(pattern, "pattern");
		
		return getPatterns().contains(pattern);
	}
	
	/**
	 * @param mask
	 * @return {@link TimeFormatter} or null
	 */
	public final TimeFormatter getFormatterByMask(final String mask) {
	
		//
		for(final TimeFormatter formatter : getTimeFormatters()) {
			
			//
			if(formatter.getMask() != null && formatter.getMask().equals(mask)) {
				
				return formatter;
			}
		}
		
		return null;
	}
	
	/**
	 * @param pattern
	 * @return {@link TimeFormatter} or null
	 */
	public final TimeFormatter getFormatterByPattern(final String pattern) {
	
		//
		for(final TimeFormatter formatter : getTimeFormatters()) {
			
			//
			if(formatter.getMask() != null && formatter.getPattern().equals(pattern)) {
				
				return formatter;
			}
		}
		
		return null;
	}
	
	/**
	 * @return the masks
	 */
	public final List<String> getMasks() {
	
		final List<String> masks = new ArrayList<String>();
		
		//
		for(final TimeFormatter formatter : getTimeFormatters()) {
			
			//
			if(formatter.getMask() != null) {
				
				masks.add(formatter.getMask());
			}
		}
		
		return masks;
	}
	
	/**
	 * @return the patterns
	 */
	public final List<String> getPatterns() {
	
		final List<String> patterns = new ArrayList<String>();
		
		//
		for(final TimeFormatter formatter : getTimeFormatters()) {
			
			patterns.add(formatter.getPattern());
		}
		
		return patterns;
	}
	
	/**
	 * @return clone of the timeFormatters
	 */
	public final List<TimeFormatter> getTimeFormatters() {
	
		return CollectionUtil.clone(timeFormatters);
	}
	
	/**
	 * @param index
	 */
	public final void remove(final int index) {
	
		//
		if(index < 0 || index >= timeFormatters.size()) {
			
			throw new IndexOutOfBoundsException(I18N.parameter().invalidRangeIndex(0, timeFormatters.size() - 1));
		}
		
		timeFormatters.remove(index);
	}
	
	/**
	 * @param pattern
	 */
	public final void remove(final String pattern) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(pattern, "pattern");
		
		int count = -1;
		int index = -1;
		
		//
		for(final TimeFormatter formatter : timeFormatters) {
			
			count++;
			
			//
			if(formatter.getPattern().equals(pattern)) {
				
				index = count;
				
				break;
			}
		}
		
		//
		if(index > -1) {
			
			remove(index);
		}
	}
}
